home *** CD-ROM | disk | FTP | other *** search
- Path: news.xnet.com!news-admin
- From: Gary Jenkins <grj@jenkins.xnet.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Output to file vs. cout
- Date: Mon, 25 Mar 1996 20:04:22 -0600
- Organization: XNet - A Full Service Internet Provider - (708) 983-6064
- Message-ID: <315750A6.357E152F@jenkins.xnet.com>
- References: <JL.96Mar7165152@thyme.id.dth.dk> <4i18mg$ba5@hpbblb.bbn.hp.com> <4impgh$jnl@is-news.gov.ab.ca> <4in7d5$9el@thales.nmia.com>
- NNTP-Posting-Host: jenkins.xnet.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=iso-8859-1
- Content-Transfer-Encoding: 8bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.0 i486)
-
- Gilbert Healton wrote:
- >
- > Ron Orr (orr@lisd.env.gov.ab.ca) wrote:
- > : Matthias Dittrich (matti) wrote:
- > : : jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
- > : : >
- > : : >Here's another of the probs that comes from porting C to C++:
- > : : >
- > : : >- I want to be able to redirect program output to either a file or cout.
- > : : > Typically this would be selected by a commandline option ("-o outfile").
- > : : >
- > : : >- In C I would do something like this:
- > : : >
- > : : >
- > : : > main()
- > : : > {
- > : : > FILE *outputfile;
- > : : >
- > : : > if (commandline == do_output_to_file)
- > : : > outputfile = stdout;
- > : : > else
- > : : > outputfile = fopen(passed_filename, "w");
- > : : >
- > : : > fprintf(outfile, "Hello world\n");
- > : : >
- > : : > fclose(outfile); /* Maybe - not allways necassary */
- > : : > }
- > : : >
- > : : >- The question is: How is this done in C++ ?
- > : : >
- > : : >.......
- > : : ofstream outfile(passed_filename); /* this constructor opens your file */
- > : : outfile << "Hello world" << endl; /* writes the string */
- > :
- > : : The destructor closes the file and of course you should do some error checking.
- > :
- >
- > I, too, am looking for a solution to this. It seems it is taking me longer
- > to get all the ins and outs of streams than the C++ language..
- >
- > The ability to have a stream that you open, and close/reopen, at various points
- > of the file is also of interest.
- >
- > --
- > ----- Computer Consulting / Web Pages ----- http://www.nmia.com/~ghealton/
- > These opinions are my own. Life is learning and I may retract, modify,
- > even attack, my previous ideas at any time without notice.
-
-
- How about this:
-
- #include <fstream.h>
- #include <string.h> // strcmp()
- #include <stdlib.h> // exit()
-
- //
- // syntax:
- // progname [-o outfile]
- //
- int main(int argc, char *argv[])
- {
- // output to stdout as default
- ostream *outputStream = &cout;
-
- //
- // see if output is to a file
- //
- if (strcmp(argv[1],"-o") == 0)
- {
- static ofstream outFileStream(argv[2], ios::out);
-
- if (!outFileStream)
- {
- cerr << "Can't open file" << endl;
- exit (1);
- }
-
- outputStream = &outFileStream;
- }
-
- *outputStream << "Hello World" << endl;
-
- return 0;
- }
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Gary Jenkins "A noble spirit embiggens the smallest man"
- grj@jenkins.xnet.com - Jebediah Springfield
- grj@tellabs.com
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-